package com.pignest.hotel.ui.member;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

import com.pignest.hotel.beans.Member;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;

public class SelectMemListbox extends Listbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7514826809592136290L;
	private MemberService memberService;
	public SelectMemListbox(){
		memberService=Services.getMemberService();
	}

    private Textbox nameField;//会员姓名
    
    private String name;

	
	private Textbox mobileField;//手机
	
	private String mobile;

	
	private Textbox mailField;//邮件
	
	private String mail;
	
	private Textbox cardNoField;//卡号
	
	private String cardNo;
	
	


	public Textbox getNameField() {
		return nameField;
	}

	public void setNameField(Textbox nameField) {
		String name =nameField.getValue();
		this.name=name;
		this.nameField = nameField;
	}

	public Textbox getMobileField() {
		return mobileField;
	}

	public void setMobileField(Textbox mobileField) {
		String levelId=(String)mobileField.getValue();
		this.mobile=levelId;
	    this.query();
	}

	public Textbox getMailField() {
		return mailField;
	}

	public void setMailField(Textbox mailField) {
		this.mailField = mailField;
	}

	public Textbox getCardNoField() {
		return cardNoField;
	}

	public void setCardNoField(Textbox cardNoField) {
		this.cardNoField = cardNoField;
	}
	
	
	public void query(){
		
		    this.getItems().clear();
			StringBuffer hql=new StringBuffer();
			hql.append(" from Member b where 1=1 ");
			if(this.mobile!=null&&!this.mobile.equals(""))
		      hql.append(" and b.mobile like '%"+this.mobile+"%'");
			if(this.name!=null&&!this.name.equals(""))
				hql.append(" and b.name like '%").append(this.name).append("%'");
			List<Member> memberList =memberService.find(hql.toString());
			for(int i=0;i<memberList.size();i++){
				final Member member = memberList.get(i);
				Listitem listitem = new Listitem();
				
				listitem.setValue(member);
				listitem.setParent(this);
				listitem.appendChild(new Listcell(member.getName()));
				listitem.appendChild(new Listcell(member.getSex()));
				listitem.appendChild(new Listcell(member.getMobile()));
				if(member.getMemberLevel()!=null)
					listitem.appendChild(new Listcell(member.getMemberLevel().getMemberlevel()));
				else
					listitem.appendChild(new Listcell(""));
				listitem.appendChild(new Listcell(member.getCertificatType()));
				listitem.appendChild(new Listcell(member.getCertificateNo()));
				listitem.appendChild(new Listcell(MoneyUtil.toString(member.getBalance())));
				Integer gral =member.getIntegral();
				if(gral!=null)
					listitem.appendChild(new Listcell(gral.toString()));
				else
					listitem.appendChild(new Listcell("0"));
				listitem.appendChild(new Listcell(member.getMail()));
				listitem.appendChild(new Listcell(member.getCardNo()));
				/*listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
					public void onEvent(Event event) throws Exception {   
						
						setMember(member);
					}
				});*/
			
			}
	}
}
